
//
// 120528 - AH - Created.
// 120602 - AH - (Aura) Finished java doc.
//

package org.soekrat.engine.motor;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.soekrat.engine.motor.passingstrategies.CheckForContentPassingStrategy;
import org.soekrat.engine.motor.passingstrategies.PassingStrategy;
import org.soekrat.engine.motor.passingstrategies.SequenceBasedPassingStrategy;
import org.soekrat.engine.motor.passingstrategies.WheelPassingStrategy;
import org.soekrat.engine.orb.Soekrat2Orb;
import org.soekrat.engine.wheel.ontology.ForestOntology;
import org.soekrat.engine.wheel.ontology.MineOntology;
import org.soekrat.engine.wheel.ontology.NetworkOntology;
import org.soekrat.engine.wheel.ontology.ProductTableOntology;
import org.soekrat.environment.RequestedSemanticEffects;
import org.soekrat.environment.RequestedSemanticEffects.RequestedSemanticEffect;

/**
 * <p>
 *   Parses Semantic effect specifications for Soekrat2.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class Soekrat2SemanticEffectInterpreter 
  //implements OrbSemanticEffectParser 
{
  private boolean m_create=false;
  
  private boolean m_hear=false;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Soekrat2SemanticEffectInterpreter(){}
  
  private PassingStrategy m_ps=null;
  
  private static String c_method="getPassingStrategy()";
  
  /**
   * <p>
   *   Gets 
   *   {@link PassingStrategy} to use.
   * </p>
   * @return {@link PassingStrategy} or {@code null} if non specified.
   */
  public PassingStrategy getPassingStrategy(Soekrat2Orb orb)
  {
    if (m_ps==null)
    {
      RequestedSemanticEffects effects=
        (RequestedSemanticEffects)orb.getAttribute("semantic-effects");
      
      if (effects==null)
      {
        throw new IllegalArgumentException("No semantic effects on orb");
      }
      
      System.err.println("Soekrat2 parsing of SemanticEffects");
      System.err.println(effects);
      
      String ontologies=null;
      
      for (RequestedSemanticEffect effect : effects)
      {
        String name=effect.getName();
        String action=effect.getData("action");
        
        if (name.equals("sequence"))
        {
          ontologies=action;
        }
        else if (name.equals("echo"))
        {
          if (action.equals("create")) m_create=true;
          if (action.equals("hear"))   m_hear=true;
        }
      }
      
      if (ontologies!=null) m_ps=createWheelPs(ontologies);
      
      orb.log(this, c_method, "Created passing strategy: "+m_ps);
    }
    
    return m_ps;
  }
  
  /**
   * <p>
   *   Tells if spare wheel ontologies are to be populated.
   * </p>
   * @return {@code true} if is to, {@code false} if is not.
   */
  public boolean echoCreate(){ return m_create; }
  
  /**
   * <p>
   *   Tells if spare wheel is to be consulted.
   * </p>
   * @return {@code true} if is to be, {@code false} if is not to be.
   */
  public boolean echoHear(){ return m_hear; }
  
  private PassingStrategy createWheelPs(String seq)
  {
    WheelPassingStrategy wps=new WheelPassingStrategy();

    int turns=1;
    
    wps.setTurns(turns);
    
    setSequence(seq, wps);
    
    return wps;
  }
  
  // CURRENTLY NOT IN USE
  @SuppressWarnings("unused")
  private PassingStrategy createForcePs(String seq)
  {
    CheckForContentPassingStrategy cfcps=new CheckForContentPassingStrategy();
    
    setSequence(seq, cfcps);
    
    return cfcps;
  }
  
  private void setSequence(String seqParam, SequenceBasedPassingStrategy ps)
  {
    seqParam=(seqParam==null) ? "" : seqParam;
    
    if (seqParam.length()==0) seqParam=getDefaultOntologySequence();
    
    StringTokenizer st=new StringTokenizer(seqParam, ",");

    List<String> ontNames=new ArrayList<String>();
    
    while (st.hasMoreTokens()) ontNames.add(st.nextToken().trim());
    
    ps.setSequence(ontNames);
  }
  
  private static String getDefaultOntologySequence()
  {
    StringBuilder sb=new StringBuilder();
    
    sb.append(ProductTableOntology.class.getName()).append(", ")
      .append(NetworkOntology.class.getName()).append(", ")
      .append(ForestOntology.class.getName()).append(", ")
      .append(MineOntology.class.getName());
    
    return sb.toString();
  }
  
}
